/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.display.TransitionImageDisplayer;
import me.panpf.sketch.process.MaskImageProcessor;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Locale;
import java.util.Optional;

/**
 * MaskImageProcessorTestPageView
 */
public class MaskImageProcessorTestPageView extends AbstractPageView {
    private SketchImageView sketchImageView;
    private Slider mSeekBar;
    private Text mTextMask;

    public MaskImageProcessorTestPageView(Context context, String name) {
        super(context, name);
    }

    @Override
    public void initView() {
        super.setRootView(loadView());

        sketchImageView = (SketchImageView) getRootView().findComponentById(ResourceTable.Id_image_mask);
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(super.getContext());
        DisplayAttributes displayAttributes = display.get().getAttributes();
        sketchImageView.getOptions().setMaxSize(displayAttributes.width / 2, displayAttributes.height / 2);
        // sketchImageView.getOptions().setDisplayer(new TransitionImageDisplayer());

        mTextMask = (Text) getRootView().findComponentById(ResourceTable.Id_text_mask);
        mSeekBar = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_mask);
        mSeekBar.setProgressValue(15);
        mSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b) {
                mTextMask.setText(String.format("%d/%d", i1, 100, Locale.ENGLISH));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                apply();
            }
        });

        apply();
    }

    private Component loadView() {
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(super.getContext()).parse
                (ResourceTable.Layout_mask_page_view, null, false);

        return layout;
    }

    private void apply() {
        int alpha = mSeekBar.getProgress() * 255 / 100;
        int maskColor = Color.argb(alpha, 0, 0, 0);
        sketchImageView.getOptions().setProcessor(new MaskImageProcessor(maskColor));
        sketchImageView.displayResourceImage(ResourceTable.Media_mask);
    }

}
